package com.powerunion.datacollection.service;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.FormGroupHeaderDao;
import com.powerunion.datacollection.model.jsonObject.GroupHeaderInfo;
import com.powerunion.util.log.LogHelper;

public class FormGroupHeaderManager {
	private static Logger logger = LogHelper.getInstance().getLogger(
			FormGroupHeaderManager.class);
	private FormGroupHeaderDao dao = null;

	public FormGroupHeaderManager() {
		super();
		this.dao = (FormGroupHeaderDao) DaoFactory.getInstance().getDao(
				FormGroupHeaderDao.class);
	}

	public boolean save(String headerInfo,String formId) throws Exception  {
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		// 参数为空
		if (StringUtils.isEmpty(headerInfo)) {
			String[] args = { "headerInfo" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter headerInfo NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		GroupHeaderInfo form = new GroupHeaderInfo();
		form.setFormId(formId);
		form.setHeaderCont(headerInfo);
		
		return this.dao.save(form);
	}
	
	public String getGroupHeaderInfo(String fsetId) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(fsetId)) {
			String[] args = { "fsetId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter fsetId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		GroupHeaderInfo form = new GroupHeaderInfo();
		form = this.dao.findFormByKey(fsetId);
		String headerInfo = "";
		if(form != null){
			headerInfo = form.getHeaderCont();
		}
		
		return headerInfo;
	}
	
	public boolean update4RowFixed(String formId,int rowFixed)throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.update4RowFixed(formId, rowFixed);
	}

}
